package org.paradise.dms.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.paradise.dms.pojo.SystemUser;
import org.paradise.dms.pojo.SystemUserGroup;
import org.paradise.dms.pojo.SystemUserGroupRole;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.stereotype.Service;

import com.dheaven.framework.dao.BaseHibernateDao;
import com.dheaven.framework.dao.DaoException;

/**
 * 
 * Description: 系统用户CRUD的DAO类
 * 
 * 
 * Copyright (c) 2008-2009 paraDise sTudio(DT). All Rights Reserved.
 * 
 * @version 1.0 2009-3-17 上午12:07:29 李双江（paradise.lsj@gmail.com）created
 */
@SuppressWarnings("unchecked")
@Service
public class SystemUserMgrDAOImpl extends BaseHibernateDao {
	private static Logger log = Logger.getLogger(SystemUserMgrDAOImpl.class);

	/**
	 * 
	 * Description: 增加一条系统用户
	 * 
	 * @Version1.0 2009-3-17 上午12:22:50 李双江（paradise.lsj@gmail.com）创建
	 * @return
	 */
	public boolean insertSystemUser(SystemUser systemuser) {
		try {
			this.create(systemuser);
			log.info("DMS_info:插入系统用户 " + ReflectionToStringBuilder.toString(systemuser).toString() + "成功！");
			return true;
		} catch (DaoException e) {
			e.printStackTrace();
			log.error("DMS_error：插入系统用户有误！");
			log.error("DMS_error：失败原因 " + e);
		}
		return false;
	}

	/**
	 * 
	 * Description: 取到系统里面所有的用户组信息<页面select框需要>
	 * 
	 * @Version1.0 2009-4-2 上午01:19:32 李双江（paradise.lsj@gmail.com）创建
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<SystemUserGroup> findSystemUserGroupInfo() {
		return this.find("from SystemUserGroup where systemusergroupind=1 order by systemusergroupvalue desc");
	}

	/**
	 * 
	 * Description:根据systemusergroupid去查看改组的所有用户
	 * 
	 * @Version1.0 2009-4-2 下午10:36:36 李双江（paradise.lsj@gmail.com）创建
	 * @param systemusergroupid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<SystemUser> getSystemUserByGroupID(final String systemusergroupid, int pageSize, int startRow) {
		final int pageSize1 = pageSize;
		final int startRow1 = startRow;
		return this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public List doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createQuery("from SystemUser where systemusergroupid=:systemusergroupid and systemuserind=1 order by systemusername asc");
				query.setFirstResult(startRow1);
				query.setMaxResults(pageSize1);
				query.setParameter("systemusergroupid", systemusergroupid);
				return query.list();
			}
		});
	}

	/**
	 * 
	 * Description: 根据systemusergroupid去查看改组的所有用户的总行数
	 * 
	 * @Version1.0 2009-4-2 下午10:47:09 李双江（paradise.lsj@gmail.com）创建
	 * @param systemusergroupid
	 * @return
	 * @throws DaoException
	 */
	public int getAGroupIDSysUserTotalRows(String systemusergroupid) throws DaoException {
		return this.find("from SystemUser where systemusergroupid=? and systemuserind=1 ", new Object[] { systemusergroupid }).size();
	}

	/**
	 * 
	 * Description: 根据系统用户id假删除一条系统用户
	 * 
	 * @Version1.0 2009-4-2 下午11:23:14 李双江（paradise.lsj@gmail.com）创建
	 * @param systemuserid
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String delelteSystemUserByID(String systemuserid) {
		String statusMsg = "";
		SystemUser sysu = null;
		try {
			sysu = (SystemUser) this.find("from SystemUser where systemuserid=?", new Object[] { systemuserid }).get(0);
			sysu.setSystemuserind("0");
			this.update(sysu);
			statusMsg = "删除系统成功！";
		} catch (DaoException e) {
			e.printStackTrace();
			statusMsg = "系统用户信息查询有误！";
		}
		return statusMsg;
	}

	/**
	 * 
	 * Description: 插入系统用户组权限,同时更新用户组的permissionid
	 * 
	 * @Version1.0 2009-4-4 下午06:59:20 李双江（paradise.lsj@gmail.com）创建
	 * @param sugr
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String insertSystemUserPermission(SystemUserGroupRole sugr) {
		try {
			this.create(sugr);
			String sugid = sugr.getSystemusergroupid();
			SystemUserGroupRole su = (SystemUserGroupRole) this.find("from SystemUserGroupRole su where su.systemusergroupid=?", new Object[] { sugid }).get(0);
			String supermisissionid = su.getSystemuserpermissionid();
			// 更新systemusergroup的systemuserpermissionid;
			SystemUserGroup sug = (SystemUserGroup) this.find("from SystemUserGroup s where s.systemusergroupid =?", new Object[] { sugid }).get(0);
			sug.setSystemuserpermissionid(supermisissionid);
			this.update(sug);
			return "插入用户组权限成功！";
		} catch (DaoException e) {
			log.error(e);
			return "插入用户组权限失败！";
		}
	}

	/**
	 * 
	 * Description: 更新系统用户组权限
	 * 
	 * @Version1.0 Apr 11, 2009 2:56:18 PM 李双江（paradise.lsj@gmail.com）创建
	 * @param systemusergroupid
	 * @param systemuserpermissionvalue
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public String updateSystemUserPermission(String systemusergroupid, String systemuserpermissionvalue) {
		try {
			List<SystemUserGroupRole> list = this.find("from SystemUserGroupRole where systemusergroupid=? ", new Object[] { systemusergroupid });
			if (list != null && list.size() > 0) {
				SystemUserGroupRole sugr = list.get(0);
				sugr.setSystemuserpermissionvalue(systemuserpermissionvalue);
				this.update(sugr);
				log.info("DMS_info:更新id为 " + systemusergroupid + "的系统用户组权限成功！");
				return "系统组权限更新成功！";
			}
		} catch (DaoException e) {
			log.error("DMS_error:查询id为" + systemusergroupid + "的用户组失败");
			log.error("DMS_error:失败原因:" + e);
		}
		return null;
	}

	/**
	 * 
	 * Description: 查询系统用户组总行数
	 * 
	 * @Version1.0 Apr 21, 2009 2:21:06 AM 李双江（paradise.lsj@gmail.com）创建
	 * @return
	 */
	public int getSystemUserGroupTotalRows() {
		return this.find("from SystemUserGroup where systemusergroupind=1").size();
	}

	/**
	 * 
	 * Description:分页查询系统用户组
	 * 
	 * @Version1.0 Apr 21, 2009 2:28:56 AM 李双江（paradise.lsj@gmail.com）创建
	 * @param pageSize
	 * @param startRow
	 * @return
	 */
	public List<SystemUserGroup> listAllSysUserGroupListByPage(int pageSize, int startRow) {
		final int pageSize1 = pageSize;
		final int startRow1 = startRow;
		Query query = this.createQuery("from SystemUserGroup where systemusergroupind=1");
		query.setFirstResult(startRow1);
		query.setMaxResults(pageSize1);
		return (List<SystemUserGroup>) query.list();
	}

	/**
	 * 
	 * Description: 根据systemusergroupid去删除一个系统用户组
	 * 
	 * @Version1.0 Apr 21, 2009 2:37:14 AM 李双江（paradise.lsj@gmail.com）创建
	 * @param systemusergroupid
	 * @return
	 */
	public String deleteSysUserGroupById(String systemusergroupid) {
		String statusMsg = "";
		SystemUserGroup sug = null;
		try {
			sug = (SystemUserGroup) this.find("from SystemUserGroup where systemusergroupid=?", new Object[] { systemusergroupid }).get(0);
			sug.setSystemusergroupind("0");
			this.update(sug);
			statusMsg = "删除系统用户组成功！";
			log.info("DMS_info：" + "删除id为" + systemusergroupid + "的系统用户组成功");
		} catch (DaoException e) {
			e.printStackTrace();
			statusMsg = "系统用户组信息查询有误！";
			log.error("DMS_error：系统用户组信息查询有误！");
			log.error("DMS_error：失败原因 " + e);
		}
		return statusMsg;
	}

	/**
	 * 
	 * Description: 判断系统用户是否存在
	 * 
	 * @Version1.0 May 20, 2009 2:52:12 PM 李双江（paradise.lsj@gmail.com）创建
	 * @param systemusername
	 * @return
	 */
	public boolean isSystemUserNameExists(String systemusername) {
		try {
			List<SystemUser> list = this.find("from SystemUser where systemusername=? and systemuserind=1", new Object[] { systemusername });
			if (list != null && list.size() > 0) { // 存在该用户
				return true;
			} else {
				return false;
			}
		} catch (DaoException e) {
			log.error("DMS_error：系统用户名信息查询有误！");
			log.error("DMS_error：失败原因 " + e);
			return true;
		}
	}
}
